﻿/**

 @Name: layuiQuietBlog - 静谧风格个人博客模板
 @Author: xuzhiwen
 @Copyright: layui.com

 */


layui.define(['element', 'carousel', 'laypage', 'form', 'laytpl', 'layer','util'], function (exports) {
    var $ = layui.jquery
        , laytpl = layui.laytpl
        , element = layui.element
        , carousel = layui.carousel
        , laypage = layui.laypage
        , layer = layui.layer
        , util = layui.util
        , form = layui.form;

    layui.use('code', function(){ //加载code模块
        layui.code(); //引用code方法
    });



    //分页
    if (typeof pageTotal != "undefined" && pageTotal > limit) {
        laypage.render({
            elem: 'page'
            , count: pageTotal //数据总数
            , groups: 5
            , limit: limit
            , curr: current_page
            , jump: function (obj, first) {
                //obj包含了当前分页的所有参数，比如：
                //obj.curr //得到当前页，以便向服务端请求对应页的数据。
                // obj.limit //得到每页显示的条数

                let map = {};
                let url_column_id = '';
                let url_search_name = '';

                let messagetable_id = $('#messagetable_id').val()?$('#messagetable_id').val():0;
                let messagetable_type = $('#messagetable_type').val()?$('#messagetable_type').val():0;

                let limit = '/limit/' + obj.limit;
                let page = '/page/' + obj.curr;

                //是否是栏目列表
                var column_id = $("#tabBox li.layui-this").attr('data-id');

                //首次不执行
                if (!first) {

                    if (column_id !== undefined) {
                        url_column_id = '/column_id/' + column_id;
                        location.href = pageUrl + page + limit + url_column_id + '.html';
                    } else if (search_name !== '') {
                        //是否是搜索
                        url_search_name = '/search_name/' + search_name;
                        location.href = pageUrl + page + limit + url_search_name + '.html';
                    } else if(messagetable_id !== 0){
                        location.href = pageUrl + page + limit + '.html';
                    }else {
                        location.href = pageUrl + page + limit + '.html';
                    }
                }
            }
        });
    }

    //end 提交
    $('#item-btn').on('click', function () {
        var count = $('#LAY-msg-box li').length;
        // console.log(count);

        var messageUrl = $('#message-formUrl').attr('action');

        //获取发表时间
        var data = new Date();
        var f = data.getFullYear(), m = data.getMonth(), g = data.getDate();
        time = f + '/' + m + '/' + g;

        var elemCont = $('#LAY-msg-content')
            , elemName = $('#LAY-msg-name')
            , content = elemCont.val()
            , userName = elemName.val();

        if (userName.replace(/\s/g, '') == "") {
            layer.msg('请先输入留言名');
            return elemName.focus();
        } else if(content.replace(/\s/g, '') == ""){
            layer.msg('请先输入留言');
            return elemCont.focus();
        } else {

            var view = $('#LAY-msg-tpl').html()
                //模拟数据
                , data = {
                    name: userName
                    , time: time
                    , avatar: '/static/index/img/avatar.jpg'
                    , content: util.escape(content)
                    , messagetable_id: $('#messagetable_id').val()
                    , messagetable_type: $('#messagetable_type').val()
                };

            $.post(messageUrl,data,function(res){
                var icon = {icon: 5};
                if (res.code === 0){
                    icon.icon = 1;
                    //模板渲染
                    laytpl(view).render(data, function (html) {
                        $('#LAY-msg-box').prepend(html);
                        elemCont.val('');
                    });
                    $('#count').text(count + 1);
                }
                layer.msg(res.msg, icon)
            });
        }
    });


    exports('layBlog', {});
});